/**
 * 系统管理 用户管理
 */
<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>反馈信息管理</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 搜索筛选 -->
    <el-form :inline="true" :model="formInline" class="user-search">
      <el-form-item label="搜索：">
      </el-form-item>
      <el-form-item label="">
        <el-input size="small" v-model="formInline.plId" placeholder="输入停车场ID"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button size="small" type="primary" icon="el-icon-search" @click="searchplId()">搜索</el-button>
      </el-form-item>
      <el-form-item label="">
        <el-input size="small" v-model="formInline.auId" placeholder="输入用户ID"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button size="small" type="primary" icon="el-icon-search" @click="searchauId()">搜索</el-button>
      </el-form-item>
      <el-form-item label="">
        <el-input size="small" v-model="formInline.fbTime" placeholder="输入反馈时间"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button size="small" type="primary" icon="el-icon-search" @click="searchfbTime()">搜索</el-button>
      </el-form-item>
      <el-button type="warning" plain @click="Unread()">未读</el-button>
      <el-button type="success" plain @click="Read()">已读</el-button>
    </el-form>

    <!--列表-->
    <el-table size="small" @selection-change="selectChange" :data="feedbackData" highlight-current-row v-loading="loading" border element-loading-text="拼命加载中" style="width: 100%;">
      <el-table-column type="index"  align="center"   label="编号" width="50">
      </el-table-column>
      <el-table-column align="center"  prop="plId" label="停车场ID" width="120">
      </el-table-column>
      <el-table-column align="center"  prop="auId" label="用户ID" width="120">
      </el-table-column>
      <el-table-column align="center"  prop="fbTime" label="反馈时间" width="180">
      </el-table-column>
      <el-table-column align="center"  prop="fbDesc" label="反馈信息" width="800">
      </el-table-column>
      <el-table-column align="center"  prop="fbState" label="是否已读" min-width="60">
        <template slot-scope="scope">
          <el-tag type="warning" v-if="scope.row.fbState==0">未读</el-tag>
          <el-tag type="success" v-else-if="scope.row.fbState==1">已读</el-tag>
        </template>
      </el-table-column>
      <el-table-column align="center" label="操作" min-width="200">
        <template slot-scope="scope">
          <el-button size="mini" type="success" @click="updatestate(scope.$index, scope.row)">已读</el-button>
<!--          <el-button size="mini" type="danger" @click="delfeedback(scope.$index, scope.row)">删除</el-button>-->
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页组件 -->
    <Pagination v-bind:child-msg="pageparm" @callFather="callFather"></Pagination>
  </div>
</template>

<script>
// 导入请求方法
import {
  feedbackbyauId, feedbackbyfbTime,
  feedbackbyplId, getAllFeedBack,updatestate,delfeedback,Unread,Read
} from '../../api/userMG'
import Pagination from '../../components/Pagination'
export default {
  data() {
    return {
      nshow: true, //switch开启
      fshow: false, //switch关闭
      loading: false, //是显示加载
      title: '添加用户',
      editFormVisible: false, //控制编辑页面显示与隐藏
      addFormVisible: false, //控制编辑页面显示与隐藏
      dataAccessshow: false, //控制数据权限显示与隐藏
      unitAccessshow: false, //控制所属单位隐藏与显示
      // 选择数据
      selectdata: [],
      // 请求数据参数
      formInline: {
        page: 1,
        limit: 10,
        plId: '',
        auId: '',
        fbDesc: '',
        fbTime:'',
      },
      //用户数据
      feedbackData: [],
      // 数据权限
      UserDept: [],
      defaultProps: {
        children: 'children',
        label: 'name'
      },
      // 选中
      checkmenu: [],
      //参数role
      saveroleId: '',
      // 分页参数
      pageparm: {
        currentPage: 1,
        pageSize: 10,
        total: 10
      }
    }
  },
  // 注册组件
  components: {
    Pagination
  },

  /**
   * 数据发生改变
   */
  watch: {},

  /**
   * 创建完毕
   */
  created() {
    this.getdata(this.formInline)
  },

  /**
   * 里面的方法只有被调用才会执行
   */
  methods: {
    // 获取数据方法
    getdata() {
      this.loading = true

      /***
       * 调用接口，注释上面模拟数据 取消下面注释
       */
      let param ={page: this.formInline.page,limit: this.formInline.limit}
      getAllFeedBack(param).then(res => {
          this.loading = false
          if (res.code == 0){
            console.log(res.data)
            this.feedbackData = res.data.list
            // 分页赋值
            this.pageparm.currentPage = this.formInline.page
            this.pageparm.pageSize = this.formInline.limit
            this.pageparm.total = res.data.total
          }else {
            this.$message({
              type: 'info',
              message: res.msg
            })
          }

        })

    },
    // 分页插件事件
    callFather(parm) {
      this.formInline.page = parm.currentPage
      this.formInline.limit = parm.pageSize
      this.getdata (this.formInline)
    },
    //已读未读
    Unread(){
      this.loading = true
      let param ={page: this.formInline.page,limit: this.formInline.limit}
      Unread(param).then(res => {
          this.loading = false
          if (res.code == 0){
            console.log(res.data)
            this.feedbackData = res.data.list
            // 分页赋值
            this.pageparm.currentPage = this.formInline.page
            this.pageparm.pageSize = this.formInline.limit
            this.pageparm.total = res.data.total
          }else {
            this.$message({
              type: 'info',
              message: res.msg
            })
          }

        })
    },
    Read(){
      this.loading = true
      let param ={page: this.formInline.page,limit: this.formInline.limit}
        Read(param).then(res => {
          this.loading = false
          if (res.code == 0){
            console.log(res.data)
            this.feedbackData = res.data.list
            // 分页赋值
            this.pageparm.currentPage = this.formInline.page
            this.pageparm.pageSize = this.formInline.limit
            this.pageparm.total = res.data.total
          }else {
            this.$message({
              type: 'info',
              message: res.msg
            })
          }

        })
    },
    //搜索事件
    searchplId() {
      this.loading = true
      if (this.formInline.plId != null) {
        let param ={plId: this.formInline.plId,page: this.formInline.page,limit: this.formInline.limit}
        feedbackbyplId(param).then(res => {
          this.loading = false
          if (res.code == 0){
            this.feedbackData = res.data.list
            // 分页赋值
            this.pageparm.currentPage = this.formInline.page
            this.pageparm.pageSize = this.formInline.limit
            this.pageparm.total = res.data.total
          }else {
            this.$message({
              type: 'info',
              message: res.msg
            })
          }

        })
      }
    },
    searchauId() {
      this.loading = true
      if (this.formInline.auId != null) {
        let param ={auId: this.formInline.auId,page: this.formInline.page,limit: this.formInline.limit}
        feedbackbyauId(param).then(res => {
          this.loading = false
          if (res.code == 0){
            console.log(res.data)
            this.feedbackData = res.data.list
            // 分页赋值
            this.pageparm.currentPage = this.formInline.page
            this.pageparm.pageSize = this.formInline.limit
            this.pageparm.total = res.data.total
          }else {
            this.$message({
              type: 'info',
              message: res.msg
            })
          }

        })
      }
    },
    searchfbTime() {
      this.loading = true
      if (this.formInline.fbTime != null) {
        let param ={fbTime: this.formInline.fbTime,page: this.formInline.page,limit: this.formInline.limit}
        feedbackbyfbTime(param).then(res => {
          this.loading = false
          if (res.code == 0){
            console.log(res.data)
            this.feedbackData = res.data.list
            // 分页赋值
            this.pageparm.currentPage = this.formInline.page
            this.pageparm.pageSize = this.formInline.limit
            this.pageparm.total = res.data.total
            this.formInline.fbTime == null
          }else {
            this.$message({
              type: 'info',
              message: res.msg
            })
          }

        })
      }
    },
    // 删除用户
    delfeedback(index, row) {
      this.$confirm('确定要删除吗?', '信息', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          // 注销
          console.log(row.fbId)
          let param ={fbId: row.fbId}
          delfeedback(param).then(res => {
            if (res.code ==0) {
              this.$message({
                type: 'success',
                message: '删除成功!'
              })
              this.getdata(this.formInline)
            } else {
              this.$message({
                type: 'info',
                message: res.msg
              })
            }
          })
            .catch(err => {
              this.loading = false
              this.$message.error('删除失败，请稍后再试！')
            })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消！'
          })
        })
    },
    //修改已读
    updatestate(index, row) {
      this.$confirm('确定要进行此操作吗?', '信息', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          // 注销
          let param ={fbId: row.fbId,fbState: 1}
          console.log(param)
          updatestate(param).then(res => {
            if (res.code ==0) {
              this.$message({
                type: 'success',
                message: '操作成功!'
              })
              this.getdata(this.formInline)
            } else {
              this.$message({
                type: 'info',
                message: res.msg
              })
            }
          })
            .catch(err => {
              this.loading = false
              this.$message.error('操作失败，请稍后再试！')
            })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消！'
          })
        })
    },
    // 修改type
    editType: function(index, row) {
      this.loading = true
      let parm = {
        lock: '',
        userId: '',
        token: localStorage.getItem('logintoken')
      }
      parm.userId = row.userId
      let lock = row.isLock
      if (lock == 'N') {
        parm.lock = 'Y'
      } else {
        parm.lock = 'N'
      }
      // 修改状态
      userLock(parm).then(res => {
        this.loading = false
        if (res.success == false) {
          this.$message({
            type: 'info',
            message: res.msg
          })
        } else {
          this.$message({
            type: 'success',
            message: '状态修改成功'
          })
          this.getdata(this.formInline)
        }
      })
    },
    // 编辑、添加提交方法
    submitForm(editData) {
      this.$refs[editData].validate(valid => {
        if (valid) {
          // 请求方法
          userSave(this.editForm)
            .then(res => {
              this.editFormVisible = false
              this.loading = false
              if (res.success) {
                this.getdata(this.formInline)
                this.$message({
                  type: 'success',
                  message: '数据保存成功！'
                })
              } else {
                this.$message({
                  type: 'info',
                  message: res.msg
                })
              }
            })
            .catch(err => {
              this.editFormVisible = false
              this.loading = false
              this.$message.error('保存失败，请稍后再试！')
            })
        } else {
          return false
        }
      })
    },
    handleClick(data, checked, node) {
      if (checked) {
        this.$refs.tree.setCheckedNodes([])
        this.$refs.tree.setCheckedNodes([data])
        this.unitparm.deptId = data.id
        this.unitparm.deptName = data.name
        //交叉点击节点
      } else {
      }
    },

    // 选择复选框事件
    selectChange(val) {
      this.selectdata = val
    },
    // 删除用户
    // deleteUser(index, row) {
    //   this.$confirm('确定要删除吗?', '信息', {
    //     confirmButtonText: '确定',
    //     cancelButtonText: '取消',
    //     type: 'warning'
    //   })
    //     .then(() => {
    //       // 删除
    //       userDelete(row.id)
    //         .then(res => {
    //           if (res.success) {
    //             this.$message({
    //               type: 'success',
    //               message: '数据已删除!'
    //             })
    //             this.getdata(this.formInline)
    //           } else {
    //             this.$message({
    //               type: 'info',
    //               message: res.msg
    //             })
    //           }
    //         })
    //         .catch(err => {
    //           this.loading = false
    //           this.$message.error('数据删除失败，请稍后再试！')
    //         })
    //     })
    //     .catch(() => {
    //       this.$message({
    //         type: 'info',
    //         message: '已取消删除！'
    //       })
    //     })
    // },
    // 重置密码
    resetpwd(index, row) {
      this.resetpsd.userId = row.userId
      this.$confirm('确定要重置密码吗?', '信息', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          userPwd(this.resetpsd)
            .then(res => {
              if (res.success) {
                this.$message({
                  type: 'success',
                  message: '重置密码成功！'
                })
                this.getdata(this.formInline)
              } else {
                this.$message({
                  type: 'info',
                  message: res.msg
                })
              }
            })
            .catch(err => {
              this.loading = false
              this.$message.error('重置密码失败，请稍后再试！')
            })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '取消重置密码！'
          })
        })
    },
    // 数据权限
    dataAccess: function(index, row) {
      this.dataAccessshow = true
      this.saveroleId = row.userId
      UserDeptTree(row.userId)
        .then(res => {
          if (res.data.success) {
            this.checkmenu = this.changemenu(res.data.data)
            this.UserDept = this.changeArr(res.data.data)
          } else {
            this.$message({
              type: 'info',
              message: res.data.msg
            })
          }
        })
        .catch(err => {
          this.loading = false
          this.$message.error('获取权限失败，请稍后再试！')
        })
    },
    //数据格式化
    changeArr(data) {
      var pos = {}
      var tree = []
      var i = 0
      while (data.length != 0) {
        if (data[i].pId == 0) {
          tree.push({
            id: data[i].id,
            name: data[i].name,
            pId: data[i].pId,
            open: data[i].open,
            checked: data[i].checked,
            children: []
          })
          pos[data[i].id] = [tree.length - 1]
          data.splice(i, 1)
          i--
        } else {
          var posArr = pos[data[i].pId]
          if (posArr != undefined) {
            var obj = tree[posArr[0]]
            for (var j = 1; j < posArr.length; j++) {
              obj = obj.children[posArr[j]]
            }

            obj.children.push({
              id: data[i].id,
              name: data[i].name,
              pId: data[i].pId,
              open: data[i].open,
              checked: data[i].checked,
              children: []
            })
            pos[data[i].id] = posArr.concat([obj.children.length - 1])
            data.splice(i, 1)
            i--
          }
        }
        i++
        if (i > data.length - 1) {
          i = 0
        }
      }
      return tree
    },
    // 选中菜单
    changemenu(arr) {
      let change = []
      for (let i = 0; i < arr.length; i++) {
        if (arr[i].checked) {
          change.push(arr[i].id)
        }
      }
      return change
    },
    // 菜单权限-保存
    menuPermSave() {
      let parm = {
        userId: this.saveroleId,
        deptIds: ''
      }
      let node = this.$refs.tree.getCheckedNodes()
      let moduleIds = []
      if (node.length != 0) {
        for (let i = 0; i < node.length; i++) {
          moduleIds.push(node[i].id)
        }
        parm.deptIds = JSON.stringify(moduleIds)
      }
      UserDeptSave(parm)
        .then(res => {
          if (res.success) {
            this.$message({
              type: 'success',
              message: '权限保存成功'
            })
            this.dataAccessshow = false
            this.getdata(this.formInline)
          } else {
            this.$message({
              type: 'info',
              message: res.msg
            })
          }
        })
        .catch(err => {
          this.loading = false
          this.$message.error('权限保存失败，请稍后再试！')
        })
    },
    // 下线用户
    offlineUser(index, row) {
      this.$confirm('确定要让' + row.userName + '用户下线吗?', '信息', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          userExpireToken(row.userName)
            .then(res => {
              if (res.success) {
                this.$message({
                  type: 'success',
                  message: '用户' + row.userName + '强制下线成功！'
                })
                this.getdata(this.formInline)
              } else {
                this.$message({
                  type: 'info',
                  message: res.msg
                })
              }
            })
            .catch(err => {
              this.loading = false
              this.$message.error('用户下线失败，请稍后再试！')
            })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消'
          })
        })
    },
    // 刷新缓存
    refreshCache(index, row) {
      userFlashCache(row.userName)
        .then(res => {
          if (res.success) {
            this.$message({
              type: 'success',
              message: '刷新成功！'
            })
            this.getdata(this.formInline)
          } else {
            this.$message({
              type: 'info',
              message: res.msg
            })
          }
        })
        .catch(err => {
          this.loading = false
          this.$message.error('刷新失败，请稍后再试！')
        })
    }
  }
}
</script>

<style scoped>
.user-search {
  margin-top: 20px;
}
.userRole {
  width: 100%;
}
</style>

